<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>算法图解学习 | Mox的笔记库</title><meta name="keywords" content="Working"><meta name="author" content="MocusEZ"><meta name="copyright" content="MocusEZ"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="作为算法学习的入门书，这本书棒极了">
<meta property="og:type" content="article">
<meta property="og:title" content="算法图解学习">
<meta property="og:url" content="https://www.mocusez.site/posts/ed41.html">
<meta property="og:site_name" content="Mox的笔记库">
<meta property="og:description" content="作为算法学习的入门书，这本书棒极了">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3329910212,2075032998&fm=26&gp=0.jpg">
<meta property="article:published_time" content="2020-11-22T03:18:51.000Z">
<meta property="article:modified_time" content="2020-11-22T03:18:51.000Z">
<meta property="article:author" content="MocusEZ">
<meta property="article:tag" content="Working">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3329910212,2075032998&fm=26&gp=0.jpg"><link rel="shortcut icon" href="/img/title.jpg"><link rel="canonical" href="https://www.mocusez.site/posts/ed41"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c85c9eaebc158345532b86397a6dded9";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '算法图解学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-11-22 11:18:51'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="Mox的笔记库" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链&amp;私人收藏</span></a></div><div class="menus_item"><a class="site-page" href="/board/"><i class="fa-fw fas fa-user"></i><span> 留言板</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3329910212,2075032998&amp;fm=26&amp;gp=0.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Mox的笔记库</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链&amp;私人收藏</span></a></div><div class="menus_item"><a class="site-page" href="/board/"><i class="fa-fw fas fa-user"></i><span> 留言板</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">算法图解学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-11-22T03:18:51.000Z" title="发表于 2020-11-22 11:18:51">2020-11-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-11-22T03:18:51.000Z" title="更新于 2020-11-22 11:18:51">2020-11-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%97%A5%E5%B8%B8%E7%AC%94%E8%AE%B0/">日常笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="算法图解学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>作为算法学习的入门书，这本书棒极了</p>
<span id="more"></span>



<p><strong>本书用的log均为2为底</strong></p>
<p>很多文本直接参考了知乎专栏  <a target="_blank" rel="noopener external nofollow noreferrer" href="https://zhuanlan.zhihu.com/p/38488791">https://zhuanlan.zhihu.com/p/38488791</a></p>
<p>GPS使用图算法来计算前往目的地的最短路径，在6，7，8章介绍</p>
<h3 id="1-2-二分查找"><a href="#1-2-二分查找" class="headerlink" title="1.2 二分查找"></a>1.2 二分查找</h3><p>首先，查找不是排序</p>
<p>折半查找要求线性表必须采用顺序存储结构，而且表中元素按关键字<strong>有序排列</strong></p>
<p>对半开，比楞查效率更高</p>
<p>对数是幂运算的逆运算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">大前提是先按大小排好序</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">binary_search</span>(<span class="params"><span class="built_in">list</span>, item</span>):</span><br><span class="line">  <span class="comment"># low and high keep track of which part of the list you&#x27;ll search in.</span></span><br><span class="line">  low = <span class="number">0</span></span><br><span class="line">  high = <span class="built_in">len</span>(<span class="built_in">list</span>) - <span class="number">1</span></span><br><span class="line"></span><br><span class="line">  <span class="comment"># While you haven&#x27;t narrowed it down to one element ...</span></span><br><span class="line">  <span class="keyword">while</span> low &lt;= high:</span><br><span class="line">    <span class="comment"># ... check the middle element</span></span><br><span class="line">    mid = (low + high) // <span class="number">2</span></span><br><span class="line">    guess = <span class="built_in">list</span>[mid]</span><br><span class="line">    <span class="comment"># Found the item.</span></span><br><span class="line">    <span class="keyword">if</span> guess == item:</span><br><span class="line">      <span class="keyword">return</span> mid</span><br><span class="line">    <span class="comment"># The guess was too high.</span></span><br><span class="line">    <span class="keyword">if</span> guess &gt; item:</span><br><span class="line">      high = mid - <span class="number">1</span></span><br><span class="line">    <span class="comment"># The guess was too low.</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">      low = mid + <span class="number">1</span></span><br><span class="line"></span><br><span class="line">  <span class="comment"># Item doesn&#x27;t exist</span></span><br><span class="line">  <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">my_list = [<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">11</span>,<span class="number">13</span>]</span><br><span class="line"><span class="built_in">print</span>(binary_search(my_list, <span class="number">13</span>)) <span class="comment"># =&gt; 1</span></span><br><span class="line"><span class="comment"># 返回所在位置</span></span><br><span class="line"><span class="comment"># &#x27;None&#x27; means nil in Python. We use to indicate that the item wasn&#x27;t found.</span></span><br><span class="line"><span class="built_in">print</span>(binary_search(my_list, -<span class="number">1</span>)) <span class="comment"># =&gt; None</span></span><br></pre></td></tr></table></figure>



<h3 id="1-3大O表示法"><a href="#1-3大O表示法" class="headerlink" title="1.3大O表示法"></a>1.3大O表示法</h3><p>O 是 <code>Operation</code> 的简写</p>
<p><strong>并非以秒为单位，基准值并不确定</strong></p>
<p>例如,假设列表包含 n 个元素。简单查找需要检查每个元素,因此需要执行 n 次操作。使用大O表示法, 这个运行时间为<code>O(n)</code></p>
<p>通过比较操作数，指出算法运行的增速</p>
<p><strong>在二分查找中，最多需要检查log n个元素</strong> </p>
<p>时间复杂度：O（log2n）</p>
<h4 id="1-3-3-大O表示法指出最糟情况下的运行时间"><a href="#1-3-3-大O表示法指出最糟情况下的运行时间" class="headerlink" title="1.3.3 大O表示法指出最糟情况下的运行时间"></a>1.3.3 大O表示法指出最糟情况下的运行时间</h4><p>O(log n),对数时间</p>
<p>O(n),线性时间</p>
<p>O(n*log n),快速排序</p>
<p>O(n^2),选择排序，冒泡排序</p>
<p>O(n!),旅行商问题</p>
<p>大O表示法忽略1&#x2F;2这样的常数</p>
<p>算法常常和<strong>数据结构</strong>挂钩。在介绍数据结构之前，我们需要先理解内存的工作原理，这样有助于我们理解数据结构。</p>
<h2 id="2-选择排序"><a href="#2-选择排序" class="headerlink" title="2 选择排序"></a>2 选择排序</h2><h3 id="2-2-数组和链表"><a href="#2-2-数组和链表" class="headerlink" title="2.2 数组和链表"></a>2.2 数组和链表</h3><h4 id="2-2-2-数组"><a href="#2-2-2-数组" class="headerlink" title="2.2.2 数组"></a>2.2.2 数组</h4><p>要读取链表最后一个元素时，必须先从第一个元素开始读起</p>
<h4 id="2-2-4-在中间插入"><a href="#2-2-4-在中间插入" class="headerlink" title="2.2.4 在中间插入"></a>2.2.4 在中间插入</h4><p>链表插入更简单，数组插入时要把后面的元素向后移</p>
<h4 id="2-2-5-删除"><a href="#2-2-5-删除" class="headerlink" title="2.2.5 删除"></a>2.2.5 删除</h4><p>链表修改地址即可</p>
<p>数组在删除元素后，将后面的元素向前移</p>
<p>数组支持随机访问</p>
<p>一种特殊的数据：<strong>链表数组</strong></p>
<p>这个数组包含26个元素，每个元素指向一个链表</p>
<h3 id="2-3-选择排序"><a href="#2-3-选择排序" class="headerlink" title="2.3 选择排序"></a>2.3 选择排序</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">运行时间O(n*1/2*n)</span><br></pre></td></tr></table></figure>

<p>我觉得n(n+1)&#x2F;2</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Finds the smallest value in an array</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">findSmallest</span>(<span class="params">arr</span>):</span><br><span class="line">  <span class="comment"># Stores the smallest value</span></span><br><span class="line">  smallest = arr[<span class="number">0</span>]</span><br><span class="line">  <span class="comment"># Stores the index of the smallest value</span></span><br><span class="line">  smallest_index = <span class="number">0</span></span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="built_in">len</span>(arr)):</span><br><span class="line">    <span class="keyword">if</span> arr[i] &lt; smallest:</span><br><span class="line">      smallest_index = i</span><br><span class="line">      smallest = arr[i]      </span><br><span class="line">  <span class="keyword">return</span> smallest_index</span><br><span class="line"></span><br><span class="line"><span class="comment"># Sort array</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">selectionSort</span>(<span class="params">arr</span>):</span><br><span class="line">  newArr = []</span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(arr)):</span><br><span class="line">      <span class="comment"># Finds the smallest element in the array and adds it to the new array</span></span><br><span class="line">      smallest = findSmallest(arr)</span><br><span class="line">      newArr.append(arr.pop(smallest))</span><br><span class="line">  <span class="keyword">return</span> newArr</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(selectionSort([<span class="number">5</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">10</span>]))</span><br></pre></td></tr></table></figure>

<p>会有警告，i没有使用上，实际情况是i只用来循环</p>
<p>(C语言版本写的不好，原数列没有删掉，抽出的数用一个远大于数组的数取代，大O表示法应该是n的n次方)</p>
<h2 id="3-递归"><a href="#3-递归" class="headerlink" title="3 递归"></a>3 递归</h2><p>就是自己调用自己</p>
<p>“如果使用循环，程序性能可能更高；</p>
<p>如果使用递归，程序可能更容易理解”</p>
<h3 id="3-2-基线条件和递归条件"><a href="#3-2-基线条件和递归条件" class="headerlink" title="3.2 基线条件和递归条件"></a>3.2 基线条件和递归条件</h3><p>递归条件：使函数调用自己的条件</p>
<p>基线条件：使函数不调用自己的条件</p>
<h3 id="3-3-栈"><a href="#3-3-栈" class="headerlink" title="3.3 栈"></a>3.3 栈</h3><p>后进先出，内存的一种储存方式</p>
<p>「栈」是一种先入后出（FILO）简单的数据结构。「调用栈」是计算机在内部使用的栈。当调用一个函数时，计算机会把函数调用所涉及到的所有变量都存在内存中。如果函数中继续调用函数，计算机会为第二个函数页分配内存并存在第一个函数上方。当第二个函数执行完时，会再回到第一个函数的内存处。</p>
<h4 id="3-3-1-调用栈"><a href="#3-3-1-调用栈" class="headerlink" title="3.3.1 调用栈"></a>3.3.1 调用栈</h4><p>所有函数调用都进入调用栈</p>
<p>写在函数里面的函数在最上层</p>
<p>即<strong>调用一个另函数时，当前函数暂停并处在未完成的状态</strong></p>
<p>这个栈用于存储多个函数的变量，被称为<strong>调用栈</strong></p>
<h4 id="3-3-2-递归调用栈"><a href="#3-3-2-递归调用栈" class="headerlink" title="3.3.2 递归调用栈"></a><strong>3.3.2 递归调用栈</strong></h4><p>递归函数也使用调用栈，所以迭代过多会造成<strong>堆栈溢出</strong></p>
<h2 id="4-快速排序"><a href="#4-快速排序" class="headerlink" title="4 快速排序"></a>4 快速排序</h2><h3 id="4-1-分而治之"><a href="#4-1-分而治之" class="headerlink" title="4.1 分而治之"></a>4.1 分而治之</h3><p>一种著名的递归式问题解决方案</p>
<p>第一，找出基线条件，<strong>这种条件尽可能简单</strong></p>
<p>第二，不断将问题分解，直到符合基线条件</p>
<p>递归一定要记录状态吗？？</p>
<h3 id="4-2-快速排序"><a href="#4-2-快速排序" class="headerlink" title="4.2 快速排序"></a>4.2 快速排序</h3><p>C语言标准库中的qsort实现的就是快速排序</p>
<p>基线条件为 空或只包含一个元素（因为不需要排序）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">quicksort</span>(<span class="params">array</span>):</span><br><span class="line">  <span class="keyword">if</span> <span class="built_in">len</span>(array) &lt; <span class="number">2</span>:</span><br><span class="line">    <span class="comment"># base case, arrays with 0 or 1 element are already &quot;sorted&quot;</span></span><br><span class="line">    <span class="keyword">return</span> array</span><br><span class="line">  <span class="keyword">else</span>:</span><br><span class="line">    <span class="comment"># recursive case</span></span><br><span class="line">    pivot = array[<span class="number">0</span>]</span><br><span class="line">    <span class="comment"># sub-array of all the elements less than the pivot</span></span><br><span class="line">    less = [i <span class="keyword">for</span> i <span class="keyword">in</span> array[<span class="number">1</span>:] <span class="keyword">if</span> i &lt;= pivot]</span><br><span class="line">    <span class="comment"># 这是什么操作（python还有这种写法？）</span></span><br><span class="line">    <span class="comment"># 从1开始，往后循环</span></span><br><span class="line">    <span class="comment"># sub-array of all the elements greater than the pivot</span></span><br><span class="line">    greater = [i <span class="keyword">for</span> i <span class="keyword">in</span> array[<span class="number">1</span>:] <span class="keyword">if</span> i &gt; pivot]</span><br><span class="line">    <span class="keyword">return</span> quicksort(less) + [pivot] + quicksort(greater)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(quicksort([<span class="number">10</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">8</span>]))</span><br></pre></td></tr></table></figure>

<p>找基准值——分区——缩小规模到两个或一个数</p>
<h3 id="4-3-再谈大O排序法"><a href="#4-3-再谈大O排序法" class="headerlink" title="4.3 再谈大O排序法"></a>4.3 再谈大O排序法</h3><p>选择排序的时间为O(n^2)</p>
<p>而合并排序的时间为O(n logn)</p>
<p>快速排序在<strong>最坏情况</strong>下时间为O(n^2)</p>
<p>平均情况为O(n logn)</p>
<h4 id="4-3-1-比较合并排序与快速排序"><a href="#4-3-1-比较合并排序与快速排序" class="headerlink" title="4.3.1 比较合并排序与快速排序"></a>4.3.1 比较合并排序与快速排序</h4><p>大O表示法不考虑常量（单位运行时间）</p>
<p>实际上，快速查找遇上平均情况的可能性比最糟情况要高很多</p>
<p><strong>因为除非每次调动都不移动（或大部分前面不移动），都属于平均情况</strong></p>
<p>快速查找的常量比合并排序+二分查找<strong>要低</strong></p>
<h4 id="4-3-2-平均情况和最糟情况"><a href="#4-3-2-平均情况和最糟情况" class="headerlink" title="4.3.2 平均情况和最糟情况"></a><strong>4.3.2 平均情况和最糟情况</strong></h4><p>快速排序最坏的情况是初始序列已经有序，第1趟排序经过n-1次比较后，将第1个元素仍然定在原来的位置上，并得到一个长度为n-1的子序列；第2趟排序经过n-2次比较后，将第2个元素确定在它原来的位置上，又得到一个长度为n-2的子序列；以此类推，最终总的比较次数：<br>C(n) &#x3D; (n-1) + (n-2) + … + 1 &#x3D; n(n-1)&#x2F;2<br>最坏的情况下，快速排序的时间复杂度为O(n^2) </p>
<p>所以，随机选择一个数作为基准值，一般都是平均时长</p>
<p><strong>是分而治之的典范</strong></p>
<h2 id="5-散列表"><a href="#5-散列表" class="headerlink" title="5 散列表"></a>5 散列表</h2><p>最有用的数据结构之一</p>
<p>在编程语言中，存在另外一种和数组不同的复杂数据结构，比如JavaScript中的对象，或 Python 中的 字典。对应到计算机的存储上，它们可能可以对应为 散列表</p>
<p>哈希表又称散列表</p>
<h3 id="5-1-散列函数"><a href="#5-1-散列函数" class="headerlink" title="5.1 散列函数"></a>5.1 散列函数</h3><p>必须一一对应，且是确定关系</p>
<p>最理想的情况是，不同输入得到不同数字</p>
<p>散列表&#x3D;散列函数+数组？？？</p>
<p>包含额外逻辑的数据结构？？</p>
<p>数组和函数直接映射到内存，而散列表使用散列函数确定函数的储存位置</p>
<p>Python提供的散列表实现是<strong>字典</strong>（大括号）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">book = &#123;<span class="string">&quot;apple&quot;</span>: <span class="number">0.67</span>, <span class="string">&quot;milk&quot;</span>: <span class="number">1.49</span>, <span class="string">&quot;avocado&quot;</span>: <span class="number">1.49</span>&#125;</span><br><span class="line">book[<span class="string">&quot;Lisa&quot;</span>] = <span class="number">100</span></span><br><span class="line"><span class="built_in">print</span>(book)</span><br></pre></td></tr></table></figure>



<p>（而C没有提供实例）</p>
<p>散列表将<strong>键映射到值</strong></p>
<h3 id="5-2-应用案例"><a href="#5-2-应用案例" class="headerlink" title="5.2 应用案例"></a>5.2 应用案例</h3><h4 id="5-2-1-将散列表用于查找"><a href="#5-2-1-将散列表用于查找" class="headerlink" title="5.2.1 将散列表用于查找"></a>5.2.1 将散列表用于查找</h4><p>散列表是提供DNS解析的一种方式</p>
<h4 id="5-2-2-防止重复"><a href="#5-2-2-防止重复" class="headerlink" title="5.2.2 防止重复"></a>5.2.2 防止重复</h4><p>散列表与列表的最大差别在于：列表需要遍历才能查询，而散列表不需要（很明显散列表占用的资源更多）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">voted = &#123;&#125;</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">check_voter</span>(<span class="params">name</span>):</span><br><span class="line">  <span class="keyword">if</span> voted.get(name): </span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;kick them out!&quot;</span>)</span><br><span class="line">  <span class="keyword">else</span>:</span><br><span class="line">    voted[name] = <span class="literal">True</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;let them vote!&quot;</span>)</span><br><span class="line"></span><br><span class="line">check_voter(<span class="string">&quot;tom&quot;</span>)</span><br><span class="line">check_voter(<span class="string">&quot;mike&quot;</span>)</span><br><span class="line">check_voter(<span class="string">&quot;mike&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="5-2-3-将散列表用作缓存"><a href="#5-2-3-将散列表用作缓存" class="headerlink" title="5.2.3 将散列表用作缓存"></a>5.2.3 将散列表用作缓存</h4><h3 id="5-3-冲突"><a href="#5-3-冲突" class="headerlink" title="5.3 冲突"></a>5.3 冲突</h3><p>即一对一映射中，另外一边映射的是链表</p>
<p>最理想的情况是：</p>
<p>散列函数将键均匀映射到散列表的不同位置</p>
<h3 id="5-4-性能"><a href="#5-4-性能" class="headerlink" title="5.4 性能"></a>5.4 性能</h3><p>如何选择一个好的散列函数？</p>
<p>数列表的平均运行为常量时间（简单查找是线性时间，二分查找是对数时间）</p>
<p><strong>最糟情况</strong>是O(n) （为什么？目前没有找到原因？）</p>
<h4 id="5-4-1-填装因子"><a href="#5-4-1-填装因子" class="headerlink" title="5.4.1 填装因子"></a>5.4.1 填装因子</h4><p>填装因子&#x3D;散列表包含的元素数&#x2F;位置总数</p>
<p>填装因子越低，发生冲突的可能性越低，散列表性能越好</p>
<p>填装因子超过0.7就建议调整长度</p>
<p>良好的散列函数让数组中的值呈均匀分布，不过我们不用担心该如何才能构造好的散列函数，著名的<code>SHA</code> 函数，就可用作散列函数</p>
<h2 id="6-广度优先搜索"><a href="#6-广度优先搜索" class="headerlink" title="6 广度优先搜索"></a>6 广度优先搜索</h2><p>数据结构图创建网络模型</p>
<p>拓扑排序，指出节点的依赖关系</p>
<h3 id="6-1-图简介"><a href="#6-1-图简介" class="headerlink" title="6.1 图简介"></a>6.1 图简介</h3><p>将现实描绘成点线图</p>
<p>解决最短路径的算法被称为<strong>广度优先搜索</strong></p>
<h3 id="6-2-图是什么"><a href="#6-2-图是什么" class="headerlink" title="6.2 图是什么"></a>6.2 图是什么</h3><p>图由节点（node）和边（edge）组成，它模拟一组连接。一个节点可能与众多节点直接相连，这些节点被称为邻居。有向图指的是节点之间单向连接，无向图指的是节点直接双向连接。</p>
<p>图用于仿真不同的东西是如何相连的</p>
<p>在编程语言中，我们可以用散列表来抽象表示图</p>
<h3 id="6-3-广度优先搜索"><a href="#6-3-广度优先搜索" class="headerlink" title="6.3 广度优先搜索"></a>6.3 广度优先搜索</h3><p>广度优先搜索可解答两类问题：</p>
<p>第一，两个节点间存不存在路径</p>
<p>第二，两个节点间哪条路径最短</p>
<h4 id="6-3-2-队列"><a href="#6-3-2-队列" class="headerlink" title="6.3.2 队列"></a>6.3.2 队列</h4><p>即堆栈中的堆（先进先出）（FIFO）</p>
<p>队列的工作原理和现实生活中的队列完全相同，可类比为在公交车前排队，队列只支持两种操作：入队 和 出队。<br>队列是一种<strong>先进先出的</strong>（FIFO）数据结构。</p>
<p>FIFO&#x3D;First In First Out</p>
<h4 id="6-4-实现图"><a href="#6-4-实现图" class="headerlink" title="6.4 实现图"></a>6.4 实现图</h4><p>通过散列表实现，用表来实现图</p>
<p>散列表模拟图<br>散列表是一种用来模拟图的数据结构（？？？）</p>
<h4 id="6-5-实现算法"><a href="#6-5-实现算法" class="headerlink" title="6.5 实现算法"></a>6.5 实现算法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> collections <span class="keyword">import</span> deque</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">person_is_seller</span>(<span class="params">name</span>):</span><br><span class="line">      <span class="keyword">return</span> name[-<span class="number">1</span>] == <span class="string">&#x27;m&#x27;</span></span><br><span class="line"></span><br><span class="line">graph = &#123;&#125;</span><br><span class="line">graph[<span class="string">&quot;you&quot;</span>] = [<span class="string">&quot;alice&quot;</span>, <span class="string">&quot;bob&quot;</span>, <span class="string">&quot;claire&quot;</span>]</span><br><span class="line">graph[<span class="string">&quot;bob&quot;</span>] = [<span class="string">&quot;anuj&quot;</span>, <span class="string">&quot;peggy&quot;</span>]</span><br><span class="line">graph[<span class="string">&quot;alice&quot;</span>] = [<span class="string">&quot;peggy&quot;</span>]</span><br><span class="line">graph[<span class="string">&quot;claire&quot;</span>] = [<span class="string">&quot;thom&quot;</span>, <span class="string">&quot;jonny&quot;</span>]</span><br><span class="line">graph[<span class="string">&quot;anuj&quot;</span>] = []</span><br><span class="line">graph[<span class="string">&quot;peggy&quot;</span>] = []</span><br><span class="line">graph[<span class="string">&quot;thom&quot;</span>] = []</span><br><span class="line">graph[<span class="string">&quot;jonny&quot;</span>] = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">search</span>(<span class="params">name</span>):</span><br><span class="line">    search_queue = deque() <span class="comment">#定义其为队列</span></span><br><span class="line">    search_queue += graph[name]</span><br><span class="line">    <span class="comment"># This array is how you keep track of which people you&#x27;ve searched before.</span></span><br><span class="line">    searched = []</span><br><span class="line">    <span class="keyword">while</span> search_queue:</span><br><span class="line">        person = search_queue.popleft()</span><br><span class="line">        <span class="comment"># Only search this person if you haven&#x27;t already searched them.</span></span><br><span class="line">        <span class="keyword">if</span> person <span class="keyword">not</span> <span class="keyword">in</span> searched:</span><br><span class="line">            <span class="keyword">if</span> person_is_seller(person):</span><br><span class="line">                <span class="built_in">print</span>(person + <span class="string">&quot; is a mango seller!&quot;</span>)</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                search_queue += graph[person]</span><br><span class="line">                <span class="comment"># Marks this person as searched</span></span><br><span class="line">                searched.append(person)</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">search(<span class="string">&quot;you&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>全局变量定义时，无视顺序</p>
<p>但字典里的键是唯一的，理论上是不会重复的</p>
<p>如果一个人为同一级两个人的好友，则需要一个表记录下已经登记过人，否则就会导致无限循环</p>
<p>运行时间至少是O（边数）</p>
<p>再算上队列检查时间O（人数）</p>
<p>所以广度优先搜索的运行时间为O（边数+人数）</p>
<p>如果任务A依赖任务B，在列表中任务A就必须在任务B后面</p>
<p>这被称为<strong>拓扑排序</strong></p>
<p>只能往下指的图，被称作树</p>
<h2 id="7-狄克斯特拉算法"><a href="#7-狄克斯特拉算法" class="headerlink" title="7 狄克斯特拉算法"></a>7 狄克斯特拉算法</h2><p><strong>引入加权图</strong></p>
<p>环会使狄克斯特拉算法失效？？</p>
<p>最短路径不一定是最快路径，广度优先只能解决最短路径，而狄克斯特拉算法则可以解决这个问题，找出总权重最小的路径</p>
<p>找到图中最便宜的节点，并确保没有到该节点更便宜的路径</p>
<h3 id="7-1-使用狄克斯特拉算法"><a href="#7-1-使用狄克斯特拉算法" class="headerlink" title="7.1 使用狄克斯特拉算法"></a>7.1 使用狄克斯特拉算法</h3><p>第一，找出最短时间内能前往的节点，终点时间先设为无限</p>
<p>第二，对于该节点的邻居，检查是否有前往他们的更短路径，如果有则更新开销</p>
<p>第三，重复这一过程</p>
<p>第四，计算最终路径</p>
<p>我认为关键是对所有路径使用该算法</p>
<h3 id="7-2-术语"><a href="#7-2-术语" class="headerlink" title="7.2 术语"></a>7.2 术语</h3><p>狄克斯特拉算法只适用于<strong>有向无环图</strong></p>
<p>其实应该是<strong>正权重有环图</strong>，环会因为权重被抛弃？？</p>
<h3 id="7-3-换钢琴"><a href="#7-3-换钢琴" class="headerlink" title="7.3 换钢琴"></a>7.3 换钢琴</h3><h3 id="7-4-负权边"><a href="#7-4-负权边" class="headerlink" title="7.4 负权边"></a>7.4 负权边</h3><p>负权边不能使用狄克斯特拉算法</p>
<p>因为狄克斯特拉算法有这样的假设：</p>
<p>对于处理过的节点，没有前往该节点的更新路径</p>
<h3 id="7-5-实现"><a href="#7-5-实现" class="headerlink" title="7.5 实现"></a>7.5 实现</h3><p><img src="https://pic2.zhimg.com/v2-e5269eadced225a2f2a4ff9bf094e670_r.jpg"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># the graph</span></span><br><span class="line"><span class="comment"># 用于确定父连接</span></span><br><span class="line">graph = &#123;&#125;</span><br><span class="line">graph[<span class="string">&quot;start&quot;</span>] = &#123;&#125;</span><br><span class="line">graph[<span class="string">&quot;start&quot;</span>][<span class="string">&quot;a&quot;</span>] = <span class="number">6</span></span><br><span class="line">graph[<span class="string">&quot;start&quot;</span>][<span class="string">&quot;b&quot;</span>] = <span class="number">2</span></span><br><span class="line"></span><br><span class="line">graph[<span class="string">&quot;a&quot;</span>] = &#123;&#125;</span><br><span class="line">graph[<span class="string">&quot;a&quot;</span>][<span class="string">&quot;fin&quot;</span>] = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">graph[<span class="string">&quot;b&quot;</span>] = &#123;&#125;</span><br><span class="line">graph[<span class="string">&quot;b&quot;</span>][<span class="string">&quot;a&quot;</span>] = <span class="number">3</span></span><br><span class="line">graph[<span class="string">&quot;b&quot;</span>][<span class="string">&quot;fin&quot;</span>] = <span class="number">5</span></span><br><span class="line"></span><br><span class="line">graph[<span class="string">&quot;fin&quot;</span>] = &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># the costs table</span></span><br><span class="line"><span class="comment"># 用于初始化</span></span><br><span class="line">infinity = <span class="built_in">float</span>(<span class="string">&quot;inf&quot;</span>)</span><br><span class="line">costs = &#123;&#125;</span><br><span class="line">costs[<span class="string">&quot;a&quot;</span>] = <span class="number">6</span></span><br><span class="line">costs[<span class="string">&quot;b&quot;</span>] = <span class="number">2</span></span><br><span class="line">costs[<span class="string">&quot;fin&quot;</span>] = infinity</span><br><span class="line"></span><br><span class="line"><span class="comment"># the parents table</span></span><br><span class="line">parents = &#123;&#125;</span><br><span class="line">parents[<span class="string">&quot;a&quot;</span>] = <span class="string">&quot;start&quot;</span></span><br><span class="line">parents[<span class="string">&quot;b&quot;</span>] = <span class="string">&quot;start&quot;</span></span><br><span class="line">parents[<span class="string">&quot;fin&quot;</span>] = <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">processed = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">find_lowest_cost_node</span>(<span class="params">costs</span>):</span><br><span class="line">    lowest_cost = <span class="built_in">float</span>(<span class="string">&quot;inf&quot;</span>)  <span class="comment">#函数初始化</span></span><br><span class="line">    lowest_cost_node = <span class="literal">None</span></span><br><span class="line">    <span class="comment"># Go through each node.</span></span><br><span class="line">    <span class="keyword">for</span> node <span class="keyword">in</span> costs:</span><br><span class="line">        cost = costs[node]</span><br><span class="line">        <span class="comment"># If it&#x27;s the lowest cost so far and hasn&#x27;t been processed yet...</span></span><br><span class="line">        <span class="keyword">if</span> cost &lt; lowest_cost <span class="keyword">and</span> node <span class="keyword">not</span> <span class="keyword">in</span> processed:</span><br><span class="line">            <span class="comment"># ... set it as the new lowest-cost node.</span></span><br><span class="line">            lowest_cost = cost</span><br><span class="line">            lowest_cost_node = node</span><br><span class="line">    <span class="keyword">return</span> lowest_cost_node</span><br><span class="line"></span><br><span class="line"><span class="comment"># Find the lowest-cost node that you haven&#x27;t processed yet.</span></span><br><span class="line">node = find_lowest_cost_node(costs)</span><br><span class="line"><span class="comment"># If you&#x27;ve processed all the nodes, this while loop is done.</span></span><br><span class="line"><span class="keyword">while</span> node <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">    cost = costs[node]</span><br><span class="line">    <span class="comment"># Go through all the neighbors of this node.</span></span><br><span class="line">    neighbors = graph[node]</span><br><span class="line">    <span class="keyword">for</span> n <span class="keyword">in</span> neighbors.keys():</span><br><span class="line">        new_cost = cost + neighbors[n]</span><br><span class="line">        <span class="comment"># If it&#x27;s cheaper to get to this neighbor by going through this node...</span></span><br><span class="line">        <span class="keyword">if</span> costs[n] &gt; new_cost:</span><br><span class="line">            <span class="comment"># ... update the cost for this node.</span></span><br><span class="line">            costs[n] = new_cost</span><br><span class="line">            <span class="comment"># This node becomes the new parent for this neighbor.</span></span><br><span class="line">            parents[n] = node</span><br><span class="line">    <span class="comment"># Mark the node as processed.</span></span><br><span class="line">    processed.append(node)</span><br><span class="line">    <span class="comment"># Find the next node to process, and loop.</span></span><br><span class="line">    node = find_lowest_cost_node(costs)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Cost from the start to each node:&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(costs)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>首先，需要3个散列表</p>
<p>第一个表储存节点的邻居</p>
<p>第二个表储存每个节点的开销</p>
<p><strong>开销</strong>指的是从起点出发前往该节点所需要的时间</p>
<p>由于不知道终点要多久，先设为无穷大</p>
<p>python中的inf为无穷大</p>
<p>如果图中包含负权边</p>
<h3 id="7-6-小结"><a href="#7-6-小结" class="headerlink" title="7.6 小结"></a>7.6 小结</h3><p>如果图中包含负权边，使用<strong>贝尔曼-福德算法</strong></p>
<h2 id="8-贪婪算法"><a href="#8-贪婪算法" class="headerlink" title="8 贪婪算法"></a>8 贪婪算法</h2><p>寻找近似解，处理没有快速算法得问题</p>
<h3 id="8-1-教室调度问题"><a href="#8-1-教室调度问题" class="headerlink" title="8.1 教室调度问题"></a>8.1 教室调度问题</h3><p>贪婪算法即是<strong>每步都采用最优解</strong></p>
<h3 id="8-2-背包问题"><a href="#8-2-背包问题" class="headerlink" title="8.2 背包问题"></a>8.2 背包问题</h3><p>贪婪算法<strong>不一定</strong>是最优解，但应当与最优解相近</p>
<h3 id="8-3-集合覆盖问题"><a href="#8-3-集合覆盖问题" class="headerlink" title="8.3 集合覆盖问题"></a>8.3 集合覆盖问题</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># You pass an array in, and it gets converted to a set.</span></span><br><span class="line">states_needed = <span class="built_in">set</span>([<span class="string">&quot;mt&quot;</span>, <span class="string">&quot;wa&quot;</span>, <span class="string">&quot;or&quot;</span>, <span class="string">&quot;id&quot;</span>, <span class="string">&quot;nv&quot;</span>, <span class="string">&quot;ut&quot;</span>, <span class="string">&quot;ca&quot;</span>, <span class="string">&quot;az&quot;</span>])</span><br><span class="line"></span><br><span class="line">stations = &#123;&#125;</span><br><span class="line">stations[<span class="string">&quot;kone&quot;</span>] = <span class="built_in">set</span>([<span class="string">&quot;id&quot;</span>, <span class="string">&quot;nv&quot;</span>, <span class="string">&quot;ut&quot;</span>])</span><br><span class="line">stations[<span class="string">&quot;ktwo&quot;</span>] = <span class="built_in">set</span>([<span class="string">&quot;wa&quot;</span>, <span class="string">&quot;id&quot;</span>, <span class="string">&quot;mt&quot;</span>])</span><br><span class="line">stations[<span class="string">&quot;kthree&quot;</span>] = <span class="built_in">set</span>([<span class="string">&quot;or&quot;</span>, <span class="string">&quot;nv&quot;</span>, <span class="string">&quot;ca&quot;</span>])</span><br><span class="line">stations[<span class="string">&quot;kfour&quot;</span>] = <span class="built_in">set</span>([<span class="string">&quot;nv&quot;</span>, <span class="string">&quot;ut&quot;</span>])</span><br><span class="line">stations[<span class="string">&quot;kfive&quot;</span>] = <span class="built_in">set</span>([<span class="string">&quot;ca&quot;</span>, <span class="string">&quot;az&quot;</span>])</span><br><span class="line"></span><br><span class="line">final_stations = <span class="built_in">set</span>()</span><br><span class="line"><span class="comment">#   第一遍时ktwo因为长度与kone的范围一样而没被纳入</span></span><br><span class="line"><span class="keyword">while</span> states_needed: <span class="comment">#在遍历中，直到states_needed全部清空</span></span><br><span class="line">  best_station = <span class="literal">None</span></span><br><span class="line">  states_covered = <span class="built_in">set</span>()</span><br><span class="line">  <span class="keyword">for</span> station, states_for_station <span class="keyword">in</span> stations.items():  <span class="comment">##没看懂</span></span><br><span class="line">    covered = states_needed &amp; states_for_station  <span class="comment">## 取交集</span></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">len</span>(covered) &gt; <span class="built_in">len</span>(states_covered):</span><br><span class="line">      best_station = station</span><br><span class="line">      states_covered = covered</span><br><span class="line"></span><br><span class="line">  states_needed -= states_covered</span><br><span class="line">  final_stations.add(best_station)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(final_stations)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="近似算法"><a href="#近似算法" class="headerlink" title="近似算法"></a>近似算法</h3><p>贪婪算法即是<strong>一种</strong>近似算法</p>
<p>准备工作-计算答案-集合处理</p>
<p>其判断优劣的方法是：</p>
<p>1.速度有多快</p>
<p>2.得到近似解与最优解的接近程度</p>
<p><em>完美是优秀的敌人。有时候，你只需要找一个能够大致解决问题的算法，此时贪婪算法正好可派上用场，它们的实现很容易，得到的结果又与正确结果接近。这时候采用的算法又被称作近似算法。</em>  </p>
<p>快速排序应该是近似算法????</p>
<h3 id="8-4-NP完全问题"><a href="#8-4-NP完全问题" class="headerlink" title="8.4 NP完全问题"></a>8.4 NP完全问题</h3><h3 id="如何识别NP完全问题"><a href="#如何识别NP完全问题" class="headerlink" title="如何识别NP完全问题"></a>如何识别NP完全问题</h3><p>以下是NP完成问题的一些特点，可以帮我我们识别NP完全问题：</p>
<ol>
<li><em>元素较少时，算法的运行速度非常快，但是随着元素的增加，速度会变得非常慢；</em></li>
<li>涉及 所有组合 的问题通常是NP完成问题；</li>
<li><em>不能将问题分解为小问题，必须考虑各种可能的情况的问题，可能是NP完全问题；</em></li>
<li>如果问题涉及到序列且难以解决（旅行商问题中的城市序列），则可能是NP完全问题；</li>
<li><em>如果问题涉及到集合（如广播台集合）且难以解决，可能是NP完全问题；</em></li>
<li>如果问题可转换我集合覆盖问题或者旅行商问题，一定就是NP完全问题；</li>
</ol>
<h2 id="9-动态规划"><a href="#9-动态规划" class="headerlink" title="9 动态规划"></a>9 动态规划</h2><p>大事化小，小事化无</p>
<p>还有一种被称作「动态规划」的思维方式可以帮我们解决问题<br>这种思维方式的核心在于，先解决子问题，再逐步解决大问题。这也导致「动态规划」思想适用于子问题都是离散的，即不依赖其他子问题的问题。</p>
<p>动态规划使用小贴士：</p>
<ul>
<li><em>每种动态规划解决方案都设计网格；</em></li>
<li>单元格中的值通常是要优化的值；</li>
<li>每个单元格都是一个子问题</li>
</ul>
<blockquote>
<p>附：<a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.zhihu.com/question/23995189">什么是动态规划？动态规划的意义是什么？—知乎讨论</a> </p>
</blockquote>
<p>这个解答很棒！</p>
<p>动态规划只能拿和不拿整件商品，无法考虑拿走商品的一部分</p>
<p>可用于模糊搜索的寻找<strong>最长公共子串</strong></p>
<h2 id="10-K最邻近算法"><a href="#10-K最邻近算法" class="headerlink" title="10 K最邻近算法"></a>10 K最邻近算法</h2><p>余弦相似度？？ 取代 最近距离</p>
<p>本书对 KNN 也做了简单的介绍，KNN的合并观点如下</p>
<ul>
<li>KNN 用于分类和回归，需要考虑最近的邻居。</li>
<li>分类就是编组</li>
<li>回归就是预测结果</li>
<li>特征抽离意味着将物品转换为一系列可比较的数字。</li>
<li>能否挑选合适的特征事关KNN算法的成败</li>
</ul>
<h2 id="11-进一步的学习建议"><a href="#11-进一步的学习建议" class="headerlink" title="11 进一步的学习建议"></a>11 进一步的学习建议</h2><p>读完本书，对算法总算有了一个入门的理解，当然算法还有很多值得深入学习的地方，以下是作者推荐的一些方向。</p>
<ul>
<li>树</li>
<li>反向索引：搜索引擎的原理</li>
<li>傅里叶变换：傅里叶变换非常适合用于处理信号，可使用它来压缩音乐；</li>
<li>并行算法：速度提升并非线性的，并行性管理开销，负载均衡</li>
<li>MapReduce：是一种流行的分布式算法，可通过流行的开源工具 Apache Hadoop 来使用；</li>
<li>布隆过滤器和 HyperLogLog：面对海量数据，找到键对于的值是一个挑战性的事情，布隆过滤器是一种概率性的数据结构，答案可能不对也可能是正确的；其优点在于占用的存储空间很小</li>
<li>SHA 算法（secure hash algorithm）安全散列函数，可用于对比文件，检查密码</li>
<li>局部敏感的散列算法，让攻击者无法通过比较散列值是否类似来破解密码</li>
<li>Diffie-Hellman 密钥交换</li>
<li>线性规划：用于在给定约束条件下最大限度的改善制定的指标</li>
</ul>
<h3 id="Diffie-Hellman-密钥交换"><a href="#Diffie-Hellman-密钥交换" class="headerlink" title="Diffie-Hellman 密钥交换"></a>Diffie-Hellman 密钥交换</h3><p>其继任者为RSA，简单易懂的公钥，私钥加密方案</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://www.mocusez.site">MocusEZ</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.mocusez.site/posts/ed41.html">https://www.mocusez.site/posts/ed41.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc/4.0" rel="external nofollow noreferrer" target="_blank">CC BY-NC 4.0</a> 许可协议。转载请注明来自 <a href="https://www.mocusez.site" target="_blank">Mox的笔记库</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3329910212,2075032998&amp;fm=26&amp;gp=0.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/1ec2.html"><img class="prev-cover" src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=372474886,775763543&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python学习笔记</div></div></a></div><div class="next-post pull-right"><a href="/posts/414f.html"><img class="next-cover" src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=238395782,2024873914&amp;fm=26&amp;gp=0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">C语言笔记</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="waline-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/head.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">MocusEZ</div><div class="author-info__description">探索未曾设想的道路</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/mocusez" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:285918468@qq.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">迎接新的明天</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="toc-number">1.</span> <span class="toc-text">1.2 二分查找</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3%E5%A4%A7O%E8%A1%A8%E7%A4%BA%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">1.3大O表示法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-3-%E5%A4%A7O%E8%A1%A8%E7%A4%BA%E6%B3%95%E6%8C%87%E5%87%BA%E6%9C%80%E7%B3%9F%E6%83%85%E5%86%B5%E4%B8%8B%E7%9A%84%E8%BF%90%E8%A1%8C%E6%97%B6%E9%97%B4"><span class="toc-number">2.1.</span> <span class="toc-text">1.3.3 大O表示法指出最糟情况下的运行时间</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="toc-number"></span> <span class="toc-text">2 选择排序</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E6%95%B0%E7%BB%84%E5%92%8C%E9%93%BE%E8%A1%A8"><span class="toc-number">1.</span> <span class="toc-text">2.2 数组和链表</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-2-%E6%95%B0%E7%BB%84"><span class="toc-number">1.1.</span> <span class="toc-text">2.2.2 数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-4-%E5%9C%A8%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5"><span class="toc-number">1.2.</span> <span class="toc-text">2.2.4 在中间插入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-5-%E5%88%A0%E9%99%A4"><span class="toc-number">1.3.</span> <span class="toc-text">2.2.5 删除</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="toc-number">2.</span> <span class="toc-text">2.3 选择排序</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%80%92%E5%BD%92"><span class="toc-number"></span> <span class="toc-text">3 递归</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E5%9F%BA%E7%BA%BF%E6%9D%A1%E4%BB%B6%E5%92%8C%E9%80%92%E5%BD%92%E6%9D%A1%E4%BB%B6"><span class="toc-number">1.</span> <span class="toc-text">3.2 基线条件和递归条件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-%E6%A0%88"><span class="toc-number">2.</span> <span class="toc-text">3.3 栈</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-1-%E8%B0%83%E7%94%A8%E6%A0%88"><span class="toc-number">2.1.</span> <span class="toc-text">3.3.1 调用栈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-2-%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%E6%A0%88"><span class="toc-number">2.2.</span> <span class="toc-text">3.3.2 递归调用栈</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="toc-number"></span> <span class="toc-text">4 快速排序</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E5%88%86%E8%80%8C%E6%B2%BB%E4%B9%8B"><span class="toc-number">1.</span> <span class="toc-text">4.1 分而治之</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="toc-number">2.</span> <span class="toc-text">4.2 快速排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E5%86%8D%E8%B0%88%E5%A4%A7O%E6%8E%92%E5%BA%8F%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">4.3 再谈大O排序法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-1-%E6%AF%94%E8%BE%83%E5%90%88%E5%B9%B6%E6%8E%92%E5%BA%8F%E4%B8%8E%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="toc-number">3.1.</span> <span class="toc-text">4.3.1 比较合并排序与快速排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-2-%E5%B9%B3%E5%9D%87%E6%83%85%E5%86%B5%E5%92%8C%E6%9C%80%E7%B3%9F%E6%83%85%E5%86%B5"><span class="toc-number">3.2.</span> <span class="toc-text">4.3.2 平均情况和最糟情况</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E6%95%A3%E5%88%97%E8%A1%A8"><span class="toc-number"></span> <span class="toc-text">5 散列表</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E6%95%A3%E5%88%97%E5%87%BD%E6%95%B0"><span class="toc-number">1.</span> <span class="toc-text">5.1 散列函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="toc-number">2.</span> <span class="toc-text">5.2 应用案例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-1-%E5%B0%86%E6%95%A3%E5%88%97%E8%A1%A8%E7%94%A8%E4%BA%8E%E6%9F%A5%E6%89%BE"><span class="toc-number">2.1.</span> <span class="toc-text">5.2.1 将散列表用于查找</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-2-%E9%98%B2%E6%AD%A2%E9%87%8D%E5%A4%8D"><span class="toc-number">2.2.</span> <span class="toc-text">5.2.2 防止重复</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-3-%E5%B0%86%E6%95%A3%E5%88%97%E8%A1%A8%E7%94%A8%E4%BD%9C%E7%BC%93%E5%AD%98"><span class="toc-number">2.3.</span> <span class="toc-text">5.2.3 将散列表用作缓存</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-%E5%86%B2%E7%AA%81"><span class="toc-number">3.</span> <span class="toc-text">5.3 冲突</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-%E6%80%A7%E8%83%BD"><span class="toc-number">4.</span> <span class="toc-text">5.4 性能</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-4-1-%E5%A1%AB%E8%A3%85%E5%9B%A0%E5%AD%90"><span class="toc-number">4.1.</span> <span class="toc-text">5.4.1 填装因子</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2"><span class="toc-number"></span> <span class="toc-text">6 广度优先搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-%E5%9B%BE%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">6.1 图简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-2-%E5%9B%BE%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">2.</span> <span class="toc-text">6.2 图是什么</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2"><span class="toc-number">3.</span> <span class="toc-text">6.3 广度优先搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3-2-%E9%98%9F%E5%88%97"><span class="toc-number">3.1.</span> <span class="toc-text">6.3.2 队列</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-4-%E5%AE%9E%E7%8E%B0%E5%9B%BE"><span class="toc-number">3.2.</span> <span class="toc-text">6.4 实现图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-5-%E5%AE%9E%E7%8E%B0%E7%AE%97%E6%B3%95"><span class="toc-number">3.3.</span> <span class="toc-text">6.5 实现算法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E7%8B%84%E5%85%8B%E6%96%AF%E7%89%B9%E6%8B%89%E7%AE%97%E6%B3%95"><span class="toc-number"></span> <span class="toc-text">7 狄克斯特拉算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1-%E4%BD%BF%E7%94%A8%E7%8B%84%E5%85%8B%E6%96%AF%E7%89%B9%E6%8B%89%E7%AE%97%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">7.1 使用狄克斯特拉算法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2-%E6%9C%AF%E8%AF%AD"><span class="toc-number">2.</span> <span class="toc-text">7.2 术语</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-3-%E6%8D%A2%E9%92%A2%E7%90%B4"><span class="toc-number">3.</span> <span class="toc-text">7.3 换钢琴</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-4-%E8%B4%9F%E6%9D%83%E8%BE%B9"><span class="toc-number">4.</span> <span class="toc-text">7.4 负权边</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-5-%E5%AE%9E%E7%8E%B0"><span class="toc-number">5.</span> <span class="toc-text">7.5 实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-6-%E5%B0%8F%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text">7.6 小结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E8%B4%AA%E5%A9%AA%E7%AE%97%E6%B3%95"><span class="toc-number"></span> <span class="toc-text">8 贪婪算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-1-%E6%95%99%E5%AE%A4%E8%B0%83%E5%BA%A6%E9%97%AE%E9%A2%98"><span class="toc-number">1.</span> <span class="toc-text">8.1 教室调度问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="toc-number">2.</span> <span class="toc-text">8.2 背包问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3-%E9%9B%86%E5%90%88%E8%A6%86%E7%9B%96%E9%97%AE%E9%A2%98"><span class="toc-number">3.</span> <span class="toc-text">8.3 集合覆盖问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%91%E4%BC%BC%E7%AE%97%E6%B3%95"><span class="toc-number">4.</span> <span class="toc-text">近似算法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-4-NP%E5%AE%8C%E5%85%A8%E9%97%AE%E9%A2%98"><span class="toc-number">5.</span> <span class="toc-text">8.4 NP完全问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E8%AF%86%E5%88%ABNP%E5%AE%8C%E5%85%A8%E9%97%AE%E9%A2%98"><span class="toc-number">6.</span> <span class="toc-text">如何识别NP完全问题</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92"><span class="toc-number"></span> <span class="toc-text">9 动态规划</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-K%E6%9C%80%E9%82%BB%E8%BF%91%E7%AE%97%E6%B3%95"><span class="toc-number"></span> <span class="toc-text">10 K最邻近算法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-%E8%BF%9B%E4%B8%80%E6%AD%A5%E7%9A%84%E5%AD%A6%E4%B9%A0%E5%BB%BA%E8%AE%AE"><span class="toc-number"></span> <span class="toc-text">11 进一步的学习建议</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Diffie-Hellman-%E5%AF%86%E9%92%A5%E4%BA%A4%E6%8D%A2"><span class="toc-number">1.</span> <span class="toc-text">Diffie-Hellman 密钥交换</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/3e9f.html" title="RMM观察与初探"><img src="https://z1.ax1x.com/2023/10/21/piF47TA.md.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="RMM观察与初探"/></a><div class="content"><a class="title" href="/posts/3e9f.html" title="RMM观察与初探">RMM观察与初探</a><time datetime="2023-10-21T04:30:00.000Z" title="发表于 2023-10-21 12:30:00">2023-10-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5e44.html" title="计算机网络课设——UDP/TCP/TLS Socket实验"><img src="https://s1.ax1x.com/2023/09/09/pP6qXOU.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="计算机网络课设——UDP/TCP/TLS Socket实验"/></a><div class="content"><a class="title" href="/posts/5e44.html" title="计算机网络课设——UDP/TCP/TLS Socket实验">计算机网络课设——UDP/TCP/TLS Socket实验</a><time datetime="2023-09-09T07:10:00.000Z" title="发表于 2023-09-09 15:10:00">2023-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/cd44.html" title="JQuery的XSS初探"><img src="https://s1.ax1x.com/2023/09/08/pPyvO0O.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JQuery的XSS初探"/></a><div class="content"><a class="title" href="/posts/cd44.html" title="JQuery的XSS初探">JQuery的XSS初探</a><time datetime="2023-09-08T04:30:00.000Z" title="发表于 2023-09-08 12:30:00">2023-09-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5862.html" title="生产实习记录"><img src="https://s1.ax1x.com/2023/09/02/pPBH058.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="生产实习记录"/></a><div class="content"><a class="title" href="/posts/5862.html" title="生产实习记录">生产实习记录</a><time datetime="2023-09-02T13:51:00.000Z" title="发表于 2023-09-02 21:51:00">2023-09-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9a9b.html" title="Fedora-CoreOS配置与试用（2023年）"><img src="https://s1.ax1x.com/2023/08/28/pPa8tlF.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Fedora-CoreOS配置与试用（2023年）"/></a><div class="content"><a class="title" href="/posts/9a9b.html" title="Fedora-CoreOS配置与试用（2023年）">Fedora-CoreOS配置与试用（2023年）</a><time datetime="2023-08-28T11:35:00.000Z" title="发表于 2023-08-28 19:35:00">2023-08-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By MocusEZ</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://beian.miit.gov.cn/" rel="external nofollow noreferrer"  style="color:#f72b07" target="_blank">闽ICP备2021003009号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadWaline () {
  function insertCSS () {
    const link = document.createElement("link")
    link.rel = "stylesheet"
    link.href = "https://cdn.jsdelivr.net/npm/@waline/client/dist/waline.min.css"
    document.head.appendChild(link)
  }

  function initWaline () {
    const waline = Waline.init(Object.assign({
      el: '#waline-wrap',
      serverURL: 'https://waline.mocusez.site',
      pageview: ,
      dark: 'html[data-theme="dark"]',
      path: window.location.pathname,
      comment: false,
    }, null))
  }

  if (typeof Waline === 'function') initWaline()
  else {
    insertCSS()
    getScript('https://cdn.jsdelivr.net/npm/@waline/client/dist/waline.min.js').then(initWaline)
  }
}

if ('Waline' === 'Waline' || !false) {
  if (false) btf.loadComment(document.getElementById('waline-wrap'),loadWaline)
  else setTimeout(loadWaline, 0)
} else {
  function loadOtherComment () {
    loadWaline()
  }
}</script></div><script>(function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/1df8ba05.js","daovoice")
</script><script>var isChatBtn = false
daovoice('init', {
  app_id: '1df8ba05',},{
  launcher: { 
     disableLauncherIcon: isChatBtn // 悬浮 ICON 是否显示
  },
});
daovoice('update');

if (isChatBtn) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      daovoice('show')
    });
  }
  chatBtnFn()
} else {
  if (false) {
    function chatBtnHide () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: true // 悬浮 ICON 是否显示
        },
      });
    }
    function chatBtnShow () {
      daovoice('update', {},{
        launcher: { 
        disableLauncherIcon: false // 悬浮 ICON 是否显示
        },
      });
    }
  }
}</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>